<?php


namespace app\app\controller\user;


use app\model\AddressModel;
use support\Request;

class Address
{
    protected $user_id = 0;

    public function beforeAction(Request $request){
        $this->user_id = $request->user_id;
    }

    public function lst(){
        $addressModel = new AddressModel();
        $addressList = $addressModel->where(['user_id' => $this->user_id])->select();
        return success($addressList);
    }
    public function add(){
        $addressModel = new AddressModel();
        $res = $addressModel->insert(
            [ 'user_id' => $this->user_id,
                'full_name' => input('full_name'),
                'mobile' => input('mobile'),
                'province_name' => input('province_name'),
                'city_name' => input('city_name'),
                'district_name' => input('district_name'),
                'street' => input('street'),
                'address' => input('province_name').' '.input('city_name').' '.input('district_name').' '.input('street'),
                'district_id' => input('cityCode')]
        );
        return $res ? success(null,0,"添加成功") : error(1,"添加失败");
    }
    public function edt(){
        $addressModel = new AddressModel();
        $res = $addressModel->where(['accepter_id' => input('accepter_id')])->update(
            ['user_id' => $this->user_id,
                'full_name' => input('full_name'),
                'mobile' => input('mobile'),
                'province_name' => input('province_name'),
                'city_name' => input('city_name'),
                'district_name' => input('district_name'),
                'street' => input('street'),
                'address' => input('province_name').' '.input('city_name').' '.input('district_name').' '.input('street'),
                'district_id' => input('cityCode')]
        );
        return $res ? success(null,0,"修改成功") : error(1,"修改失败");

    }
    public function del(){
        if(input('accepter_id',0)<1){
            return error(1,'accepter_id is null');
        }
        $addressModel = new AddressModel();
        $res = $addressModel->where(['accepter_id' => input('accepter_id')])->delete();
        return $res ? success(null,0,"删除成功") : error(1,"删除失败");
    }
}